/**
 * <p>字段校验</p>
 * xtype = cceafvalidatevalidation

 * @module Ext-ux-cceaf-validate
 * @class cceaf.validate.Validation
 * @extends Object
 * @since 1.0
 */
Ext.define('cceaf.validate.Validation', {
	alias : 'cceaf.cceafvalidatevalidation',
	requires: [
   	    'cceaf.validate.NullCheck'
   	],
	
	/**
	 * 默认校验类型，缺省为'null'
	 *
     * @property defaultType 
     * @type String
     * @default 'null'
     */
	defaultType : 'null',
	
	/**
	 * 默认父容器
	 *
     * @property defaultFather 
     * @type panel
     * @default null
     */
	defaultFather : null,
	
	/**
	 * 校验器
	 *
     * @property items 
     * @type Array
     * @default null
     */
	items : null,
	
	/**
	* 构造函数，示例：<br>
	* <br> defaultType: 'null', //默认校验类型，缺省为null
	* <br> defaultFather: , //默认父容器
	* <br> items: items, //待校验的items，每个item如下：
	* <br> 	{
	* <br> 		itemId: null, //控件itemId
	* <br> 		name: null, //控件name, 存在itemId的话，name失效
	* <br> 		type: null, //校验类型,有 null、code、email、ip等
	* <br> 		father: panel,  //itemId指定的父容器
	* <br> 		label: '',  //label名称
	* <br> 		display: '', //检验不通过显示的内容
	* <br> 		focus: true, //校验不通过聚焦
	* <br> 	}
	*/
	constructor : function(config) {
		config = config || {};
		
		var items = config.items;
		if (!config.items) {
			alert('请设置校验器！');
			return;
		} 
		
		var defaultType = config.defaultType;
		var defaultFather = config.defaultFather;
		
		var checkItems = [];
		for(var i=0;items != null && i<items.length;i++){
			var item = items[i];
			
			var type = defaultType;
			if (item.type){
				type = item.type;
			}
			var checkType = "cceaf.validate.NullCheck";
			if (type == 'email') {
				checkType = "cceaf.validate.EmailCheck";
			} else if (type == 'ip') {
				checkType = "cceaf.validate.IPCheck";
			} ;
			
			var father = defaultFather;
			if (item.father){
				father = item.father;
			}
			var focus = item.focus;
			if (focus === undefined) {
				focus = true;
			}
//			alert(focus);
			
			checkItems[i] = Ext.create(checkType,{
				control: item.control,
				display: item.display,
				itemId: item.itemId,
				name: item.name,
				label: item.label,
				father: father,
				focus: focus
			});
		}
		
		this.checkItems = checkItems;
		
		cceaf.validate.Validation.superclass.constructor.apply(this, arguments);
	},
	
	/**
	* 对定义的每个item调用不同的校验方式进行校验
	*/
	validate: function() {
		var me = this;
		
		var result = true;
		Ext.Array.each(me.checkItems, function(item){
			if ( !item.validate() ) {
				result = false;
				return result; //停止each
			}
		});

		return result;
	}
});
